package game.engine;

import game.menu.MenuContainer;
import game.menu.MenuElement;

public class MainMenuEngine extends AbstractMenuEngine {
	public static final String KEY_NEWGAME = "NEWGAME";
	public static final String RETURN_TO_GAME = "RETURN_TO_GAME";
	public static final String KEY_OPTIONS = "OPTIONS";
	public static final String KEY_EXIT = "EXIT";

	private static MainMenuEngine SINGLETON = new MainMenuEngine();

	public static MainMenuEngine newMainMenuEngine() {
		SINGLETON = new MainMenuEngine();
		return SINGLETON;
	}

	public static MainMenuEngine singleton() {
		return SINGLETON;
	}

	private MainMenuEngine() {
		super();
	}

	@Override
	protected void actionForEnter(MenuElement selectedElement) {
		if (selectedElement == null) {
			return;
		}
		switch (selectedElement.getKey()) {
		case RETURN_TO_GAME:
			setNextEngine(GameEngine.singleton());
			break;
		case KEY_NEWGAME:
			setNextEngine(GameEngine.newGame());
			break;
		case KEY_OPTIONS:
			setNextEngine(OptionsMenuEngine.newOptionsMenuEngine());
			break;
		case KEY_EXIT:
			System.exit(0);
			break;
		}
	}

	@Override
	protected MenuContainer createContainer() {
		MenuContainer result = new MenuContainer(CONTAINER_XPOS, CONTAINER_YPOS);
		if (GameEngine.singleton() != null && !GameEngine.singleton().isGameOver() && GameEngine.singleton().isReturnAble()) {
			result.addMenuElement(RETURN_TO_GAME);
		}
		result.addMenuElement(KEY_NEWGAME);
		result.addMenuElement(KEY_OPTIONS);
		result.addMenuElement(KEY_EXIT);
		return result;
	}
}